package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonDetailController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    YxyLessonsCloudCatalogRepository catalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/cloud/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;

        RespModel respModel = new RespModel();

        YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
        if (lesson != null) {
            respModel.name = lesson.getName();
            respModel.cover = lesson.getCover();
            respModel.summary = lesson.getSummary();
            respModel.count = lesson.getCount();
            respModel.rejectReason = lesson.getRejectReason();
            respModel.downType = lesson.getDownType();
            respModel.downReason = lesson.getDownReason();
            respModel.isJingpinLesson = lesson.isJingpinLesson;
            respModel.vv = lesson.getVv() + "";

            // Integer duration = lesson.getDuration();
            // String string = StringTrimUtil.disDuration(duration);
            // respModel.duration = string;

            if (null != lesson.getUpDate()) {
                respModel.upDate = DateToolUtils.format(lesson.getUpDate(), "yyyy-MM-dd");
            }
            if (null != lesson.getDownDate()) {
                respModel.downDate = DateToolUtils.format(lesson.getDownDate(), "yyyy-MM-dd");
            }
            if (null != lesson.getCreateDate()) {
                respModel.create_date = DateToolUtils.format(lesson.getCreateDate(), "yyyy-MM-dd");
            }
            if (null != lesson.getAuditDate()) {
                respModel.auditDate = DateToolUtils.format(lesson.getAuditDate(), "yyyy-MM-dd");
            }
            respModel.status = lesson.getStatus();
            respModel.isVisible = lesson.getIsVisible();
            String catalog1 = lesson.getCatalogId1();
            String catalog2 = lesson.getCatalogId2();
            String catalog3 = lesson.getCatalogId3();
            String catalog4 = lesson.getCatalogId4();
            String catalog5 = lesson.getCatalogId5();

            YxyLessonsCloudCatalog catalog = new YxyLessonsCloudCatalog();
            if (StringUtils.isNotBlank(catalog1)) {
                catalog = catalogRepository.findById(catalog1);
                if (catalog != null) {
                    respModel.catalog1Name = catalog.getName();
                }
                // 获取不到一级目录
                else {
                    catalog1 = "";
                    catalog2 = "";
                    catalog3 = "";
                    catalog4 = "";
                    catalog5 = "";
                }
            }
            if (StringUtils.isNotBlank(catalog2)) {
                catalog = catalogRepository.findById(catalog2);
                if (catalog != null) {
                    respModel.catalog2Name = catalog.getName();
                }
                // 获取不到二级目录
                else {
                    catalog2 = "";
                    catalog3 = "";
                    catalog4 = "";
                    catalog5 = "";
                }
            }
            if (StringUtils.isNotBlank(catalog3)) {
                catalog = catalogRepository.findById(catalog3);
                if (catalog != null) {
                    respModel.catalog3Name = catalog.getName();
                }
                // 获取不到三级目录
                else {
                    catalog3 = "";
                    catalog4 = "";
                    catalog5 = "";
                }
            }
            if (StringUtils.isNotBlank(catalog4)) {
                catalog = catalogRepository.findById(catalog4);
                if (catalog != null) {
                    respModel.catalog4Name = catalog.getName();
                }
                // 获取不到四级目录
                else {
                    catalog4 = "";
                    catalog5 = "";
                }
            }
            if (StringUtils.isNotBlank(catalog5)) {
                catalog = catalogRepository.findById(catalog5);
                if (catalog != null) {
                    respModel.catalog5Name = catalog.getName();
                }
                // 获取不到五级目录
                else {
                    catalog5 = "";
                }
            }
            respModel.catalog1 = catalog1;
            respModel.catalog2 = catalog2;
            respModel.catalog3 = catalog3;
            respModel.catalog4 = catalog4;
            respModel.catalog5 = catalog5;
        }
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
    }

    public static class RespModel extends YxyApiRespModel {
        public String name = "";
        public String summary = "";
        public String cover = "";
        public String catalog1 = "";
        public String catalog2 = "";
        public String catalog3 = "";
        public String catalog4 = "";
        public String catalog5 = "";
        public String catalog1Name = "";
        public String catalog2Name = "";
        public String catalog3Name = "";
        public String catalog4Name = "";
        public String catalog5Name = "";
        public Integer status = 0;
        public Integer isVisible = 0;
        public Integer count = 0;
        public String duration = "";
        public String create_date = "";
        public String auditDate;
        public String rejectReason;
        public String upDate;
        public String downDate;
        public String downType;
        public String downReason;
        public String vv;
        public Integer isJingpinLesson;
    }
}
